<?php
$allRole = $this->allRole;
?>


<div class="ad_title_ctn">
    <h2>Thêm user</h2>
</div>
<div class="ad_content_ctn">
    <div class="control_ctn">
        <a id='btnSaveUser' class="k-button k-button-icontext k-grid-edit"><span>Lưu</span></a>
        <a id='btnSaveAndCreate' class="k-button k-button-icontext k-grid-edit"><span>Lưu và Tạo mới</span></a>
    </div>
    <fieldset style="margin: 10px 5px">
        <legend>Thông tin cơ bản</legend>
        <div id="basic_info_ctn">
            <table style="width:100%;" cellpadding="0" cellspacing="0">
                <tr>
                    <td style=" vertical-align: top;">
                        <div>

                            <div style="display: block;">
                                <div class="label_150 left">
                                    Tên (*):
                                </div>
                                <div class="left">
                                    <input id="txtName" name="txtName" style="width:250px" class="k-textbox" required placeholder="Họ và tên đầy đủ, bắt buộc" validationMessage="Chưa nhập tên người dùng">
                                    <span class="k-invalid-msg" data-for="txtName"></span>
                                </div>
                                <div class="clear"></div>
                            </div>
                            <div style="display: block;">
                                <div class="label_150 left">
                                    Tên đăng nhập(*):
                                </div>
                                <div class="left">
                                    <input id="txtUsername" name="txtUsername" style="width:250px" class="k-textbox" required placeholder="Tên đăng nhập, bắt buộc" validationMessage="Chưa nhập tên đăng nhập ">
                                    <span class="k-invalid-msg" data-for="txtUsername"></span>
                                </div>
                                <div class="clear"></div>
                            </div>
                            <div style="display: block;">
                                <div class="label_150 left">
                                    Email:
                                </div>
                                <div class="left">
                                    <input id="txtEmail" type="email" name="txtEmail" style="width:250px" class="k-textbox"  placeholder="hoanglan@exam.net" data-email-msg="Email sai định dạng">
                                    <span class="k-invalid-msg" data-for="txtEmail"></span>
                                </div>
                                <div class="clear"></div>
                            </div>
                            <div style="display: block;">
                                <div class="label_150 left">
                                    Phone number:
                                </div>
                                <div class="left">
                                    <input id="txtPhone" name="txtPhone" style="width:250px" class="k-textbox"  placeholder="0987654321" >
                                    <span class="k-invalid-msg" data-for="txtPhone"></span>
                                </div>
                                <div class="clear"></div>
                            </div>
                            <div style="display: block;">
                                <div class="label_150 left">
                                    Password(*):
                                </div>
                                <div class="left">
                                    <input id="txtPassword" type="password" name="txtPassword" style="width:250px" class="k-textbox" required validationMessage="Chưa nhập password ">
                                    <span class="k-invalid-msg" data-for="txtPassword"></span>
                                </div>
                                <div class="clear"></div>
                            </div>

                            <div style="display: block;">
                                <div class="label_150 left">
                                    Nhập lại(*):
                                </div>
                                <div class="left">
                                    <input id="txtRetypedPassword" type="password" name="txtRetypedPassword" style="width:250px" class="k-textbox" required validationMessage="Chưa nhập lại password ">
                                    <span class="k-invalid-msg" data-for="txtRetypedPassword"></span>
                                </div>
                                <div class="clear"></div>
                            </div>
                            <div style="display: block;">
                                <div class="label_150 left">
                                    Chọn role:
                                </div>
                                <div class="left" style="padding: 8px 0px">
                                    <?php
                                    foreach ($allRole as $aRole) {
                                        echo '<span>'.$aRole->name.'</span>&nbsp;<input type="checkbox" name="chkRole" value="'.$aRole->id.'">&nbsp;&nbsp;&nbsp;';
                                    }

                                    ?>
                                </div>
                                <div class="clear"></div>
                            </div>
                            <div style="display: block;">
                                <div class="label_150 left">
                                    Active:
                                </div>
                                <div class="left" style="padding: 8px 0px">
                                    <input type="checkbox" id="chkActive" checked>
                                </div>
                                <div class="clear"></div>
                            </div>

                            <div style="display: block;">
                                <div class="label_150 left">
                                    Log hoạt động:
                                </div>
                                <div class="left" style="padding: 8px 0px">
                                    <input type="checkbox" id="chkLog">
                                </div>
                                <div class="clear"></div>
                            </div>


                        </div>
                    </td>

                </tr>
            </table>
        </div>
    </fieldset>


</div>
<script type="text/javascript">
    var validator;
    $(document).ready(function(){
        validator = $("#basic_info_ctn").kendoValidator().data("kendoValidator");

        function validate(){
            return validator.validate();
        }

        function saveUser(successSave){
            if(!validate())
                return;

            name = $("#txtName").val();
            username = $("#txtUsername").val();
            email = $("#txtEmail").val();
            phone = $("#txtPhone").val();
            password = $("#txtPassword").val();
            retypedPassword = $("#txtRetypedPassword").val();

            accessRoles = [];
            $(":input[name='chkRole']").each(function() {
                if($(this).is(':checked'))
                    accessRoles.push(this.value);
            });
            active = $('#chkActive').is(':checked')?1:0;
            isLog = $('#chkLog').is(':checked')?1:0;


            $.post('/admin/user/save',{
                name:name, username:username, email:email,
                phone:phone, password:password,retypedPassword:retypedPassword,
                accessRoles:accessRoles,active:active, isLog:isLog
            },function(result){
                if (result.success){
                    successSave();
//
                }
                else{
                    $('#error_ctn').html(result.msg);
                    alert(result.msg);
                }
            },'json');
        }
        $('#btnSaveUser').click(function(){
            saveOnly();
        })

        $('#btnSaveAndCreate').click(function(){
            saveAndCreate();
        })

        function refresh(){
            $("#txtName").val('');
            $("#txtUsername").val('');
            $("#txtEmail").val('');
            $("#txtPhone").val('');
            $("#txtPassword").val('');
            $("#txtRetypedPassword").val('');

            $(":input[name='chkRole']").each(function() {
                $('#chkActive').removeAttr('checked');
            });
            $('#chkActive').attr('checked', true);
            $('#chkLog').attr('checked', true);
        }

        function saveAndCreate(){
            saveUser(function(){
                $('#error_ctn').html('Thêm user thành công');
                refresh();
            })
        }
        function saveOnly(){
            saveUser(function(){
                window.location="/admin/user";
            })
        }
    });
</script>